package sg.edu.nus.iss.team2s.jass.dao;

import java.util.*;

import sg.edu.nus.iss.team2s.jass.dto.*;
import sg.edu.nus.iss.team2s.jass.util.*;

/**
 * @author LU ZHIXIAN
 */

public class CompanyDAO extends AbstractPropertiesDAO {

	public static final String FILENAME = "Company";
	
	//get the fields, need to be overridden in the subclass
//	@Override
//	protected  String[] getFields(){
//		return new String[]{"companyId","advertiserId","showName","name","description","address","hrContact","telephone","email"};
//	}
	
	@Override
	public void add(Object obj) throws DAOException {
		Company company=(Company) obj;
		String companyName = company.getName();
		String advertiserId = company.getAdvertiserId();
		String companyId=company.getCompanyId();
		
		if (companyId.equalsIgnoreCase("")){
			throw new DAOException("Company Id is empty");	
		}
		
		if (companyName.equals("") || advertiserId.equals("")){
			throw new DAOException("Either the Company Name or Advertiser ID is NULL.");	
		}
		
		//check duplicate
		
		if (get(companyId)!=null){
			throw new DAOException("CompanyId is duplicated!");	
		}
		
		ArrayList<Object> arrList=(ArrayList<Object>)list();
		
		arrList.add(obj);
		
		update(arrList);
			
	}
	
	@Override
	public Object get(String key) throws DAOException {
		
		ArrayList<Object> arrList=(ArrayList<Object>)list();
		
		for(int i=0; i < arrList.size(); i++)  {
		Company company=(Company)arrList.get(i);
		    if (company.getCompanyId().equalsIgnoreCase(key)){
		    	return company;	
		    }
			
		}
		
		throw new DAOException("NO Company Record Found!");
	}

	@Override
	public List<Object> list() throws DAOException {

		List<Object> list;
		try{
			list= retrieve(CompanyDAO.FILENAME );	
		}
		catch(Exception exp){
			throw new DAOException(exp.getMessage(),exp.getCause());
			
		}
		return list;
	}
	public List<Object> retrieveAllCompanies(String jobAdvertiserId) throws DAOException{
		List<Object> list;
		try{
			list= retrieve(CompanyDAO.FILENAME );
			Iterator<Object> iterator = list.iterator();
			
			while(iterator.hasNext()){
				Company obj=(Company)iterator.next();
				if ( ! obj.getAdvertiserId().equalsIgnoreCase(jobAdvertiserId)){
					iterator.remove();	
				}
				
			}//while
			
		}
		catch(Exception exp){
			throw new DAOException(exp.getMessage(),exp.getCause());
			
		}
		return list;
		
	}
	@Override
	public void update(List<Object> list) throws DAOException {

		try{
			 flush(list,CompanyDAO.FILENAME );	
		}
		catch(Exception exp){
			throw new DAOException(exp.getMessage(),exp.getCause());
			
		}
	}
	
	public void delete(Object obj) throws DAOException{
	ArrayList<Object> arrList=(ArrayList<Object>)list();
		
		for(int i=0; i < arrList.size(); i++)  {
		Company company=(Company)arrList.get(i);
		    if (company.getCompanyId().equalsIgnoreCase(((Company)obj).getCompanyId())){
		    	arrList.remove(i);
		    	update(arrList);
		    	break;
		    }
			
		}
		
		throw new DAOException("NO Company Record Found!");
		
	}
}

